﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BomberManServer.Gaming.Object.Mapping;
using BomberManServer.Utils;
using BomberManServer.Gaming.Time;

namespace BomberManServer.Gaming.Object.Mapping.Items
{
    public class Deflagration : Item
    {
        private Direction direction;
        public uint length { get; private set; }
        public Bomb bomb { get; private set; }
        private uint speed;
        public Coordinates coord { get; private set; }
        //private List<Block> blocks;
        public bool ended { get; set; }
        private Logger.Handler log;


        // the deflagration has to finish in 1 seconds
        public Deflagration(Bomb bomb,Direction direction,uint speed)
        {
            this.free = true;
            this.destructible = true;
            this.propagation = true;

            this.speed = speed;
            this.bomb = bomb;
            this.direction = direction;
            this.length = 0;
            this.ended = false;
            
            //this.blocks = new List<Block>();
            //this.blocks.Add(bomb.block);
            this.coord = bomb.coord;

            log = bomb.owner.game.logger.createHandler("Deflag");
        }

		public override void actionWalk(Mapping.Block block, Avatar avatar) 
		{
            log.information("Avatar " + avatar.id + " walked in deflag and died");
            avatar.game.ruleKeeper.avatarDie(bomb.owner, avatar);
		}

        //remove the block in the list when 2 deflag cross path
        public override Item actionDeflagration(Deflagration deflag)
        {
            //this.blocks.Remove(this.bomb.owner.game.map.getBlockAt(deflag.coord));
            this.bomb.deflagedBlock.Remove(this.bomb.owner.game.map.getBlockAt(deflag.coord));
            return deflag;
        }

        public void propagate()
        {
            // check if new coord are in the map.
            this.coord = this.coord.translate(this.direction, this.speed);
			if(!this.bomb.owner.game.map.blocks.check(this.coord))
			{
                this.ended = true;
                return;
            }

            //if the block is already deflaged
			Block newBlock = this.bomb.owner.game.map.getBlockAt(coord);
            if (this.bomb.deflagedBlock.Contains(newBlock))
                return;

            // if the propagation is not allowed for the new block
            log.information("Deflag act on (" + coord.xBlock + ", " + coord.yBlock + ")");
            this.ended = !newBlock.reactionDeflagration(this);
            if (this.ended)
                return;
            
            // register the new block
            this.bomb.deflagedBlock.Add(newBlock);
            length++;
            log.information("Deflag is on (" + coord.xBlock + ", " + coord.yBlock + ")");
        }
        /*
        public void expire()
        {
            foreach (Block block in blocks)
            {
                block.item = Items.None.instance;
            }
            blocks.Clear();
            //this.bomb.deflags.Remove(this);
        }
         * */


    }
}
